import { Context } from 'koa';
import { TenantService } from '../../services/system/tenantService';
import { createError } from '../../middleware/errorHandler';
import { CreateTenantDto, UpdateTenantDto } from '../../dtos/system/TenantDto';
import { generateResponse } from '../../utils';

export class TenantController {
    private tenantService: TenantService;

    constructor() {
        this.tenantService = new TenantService();
    }

    // 获取所有租户
    getTenants = async (ctx: Context): Promise<void> => {
        try {
            let data = ctx.request.body as any
            let dics = [] as any
            if (data?.page && data?.limit) {

                dics = await this.tenantService.getTenantsWithPagination(data.page, data.limit);
                console.log(dics, 'dics');
            } else {
                dics = await this.tenantService.getAllTenants();
            }
            generateResponse(ctx, dics);
        } catch (error: any) {
            throw createError('获取字典项列表失败', 500, error);
        }
    };


    // 根据ID获取角色
    getTenantById = async (ctx: Context): Promise<void> => {
        try {
            const { id } = ctx.params;
            const tenant = await this.tenantService.getTenantById(id);

            if (!tenant) {
                throw createError('租户不存在', 404);
            }

            generateResponse(ctx, tenant);
        } catch (error: any) {
            if (error.status === 404) throw error;
            throw createError('获取租户信息失败', 500, error);
        }
    };
    
    // 创建租户
    createTenant = async (ctx: Context): Promise<void> => {
        try {
            // 从验证中间件获取已验证的数据
            const tenantData = ctx.state.validatedData as CreateTenantDto;
            const tenant = await this.tenantService.createTenant(tenantData);
            generateResponse(ctx, tenant, '租户创建成功');
        } catch (error: any) {

            throw createError('创建租户失败1', 400, error);
        }
    };

    // 更新角色
    updateTenant = async (ctx: Context): Promise<void> => {
        try {
            const { id } = ctx.params;
            // 从验证中间件获取已验证的数据
            const tenantData = ctx.state.validatedData as UpdateTenantDto;

            const tenant = await this.tenantService.updateTenant(id, tenantData);

            if (!tenant) {
                throw createError('租户不存在', 404);
            }


            generateResponse(ctx, tenant, '租户更新成功');
        } catch (error: any) {
            if (error.status === 404) throw error;
            throw createError('更新租户失败', 400, error);
        }
    };

    // 删除角色
    deleteTenant = async (ctx: Context): Promise<void> => {
        try {
            const { id } = ctx.params;
            const result = await this.tenantService.deleteTenant(id);

            if (!result) {
                throw createError('租户不存在', 404);
            }


            generateResponse(ctx, null, '租户删除成功');
        } catch (error: any) {
            if (error.status === 404) throw error;
            throw createError('删除租户失败', 500, error);
        }
    };
}